package com.loong.data.utils;

import com.loong.data.common.forehead.param.PageForeheadParam;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

/**
 * @Description: 封装es中的Page
 * @Author: LiuQun
 * @Date: 2020/12/1 11:00
 */
public class ESPageUtil {

    /**
     * 获得一个Pageable对象
     * @param pageParam 分页参数
     * @return Pageable对象
     */
    public static Pageable getPageableByParam(PageForeheadParam pageParam){
        int pageIndex = 0;
        int pageSize = 10;
        if (ObjectUtils.isNotEmpty(pageParam)){
            //构建分页条件
            if (ObjectUtils.isNotEmpty(pageParam.getPageIndex())){
                pageIndex = pageParam.getPageIndex()<=1 ? 0 : pageParam.getPageIndex()-1;
            }
            if (ObjectUtils.isNotEmpty(pageParam.getPageSize())){
                pageSize = pageParam.getPageSize()<=0 ? 10 : pageParam.getPageSize();
            }
        }
        return PageRequest.of(pageIndex,pageSize);
    }
}
